package com.flash.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.flash.constant.MessageConstant;
import com.flash.dto.SetmealDTO;
import com.flash.dto.SetmealPageQueryDTO;
import com.flash.entity.Setmeal;
import com.flash.entity.SetmealDish;
import com.flash.exception.DeletionNotAllowedException;
import com.flash.mapper.DishMapper;
import com.flash.mapper.SetmealDishMapper;
import com.flash.mapper.SetmealMapper;
import com.flash.result.PageResult;
import com.flash.service.SetMealService;
import com.flash.utils.Output;
import com.flash.vo.DishItemVO;
import com.flash.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;


    /**
     * 分页查询套餐
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO pageQueryDTO) {
        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        Page<SetmealVO>  page = setmealMapper.page(pageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void insertSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insertSetMeal(setmeal);
        Output.Output(setmeal,"新增套餐");
        if (setmealDTO.getSetmealDishes() != null  && !setmealDTO.getSetmealDishes().isEmpty()){
            setmealDTO.getSetmealDishes().forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            setmealDishMapper.insertSetMealDishes(setmealDTO.getSetmealDishes());
        }

    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetMealById(Long id) {
        Setmeal setmeal = setmealMapper.getSetMealById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> setmealDishes = setmealDishMapper.getDishesIById(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 进行套餐更新
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void updateSetMeal(SetmealDTO setmealDTO) {
        /*需要修改两个表*/
        /*将原来的setmealDish表格里面关于这个套餐的全部删除*/
        setmealDishMapper.deleteByDishId(setmealDTO.getId());
        Output.Output(setmealDTO,"updateSetMeal");
        /*将现在DTO立面将新的dishes插入进去*/
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()){
            Output.Output(setmealDishes,"updateSetMeal");
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setId(setmealDTO.getId());
            });
            setmealDishMapper.insertSetMealDishes(setmealDishes);
        }
        /*进行setmeal表的更新*/
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updatesetmeal(setmeal);
    }

    @Override
    /**
     * 进行套餐删除
     * @param ids
     * @return
     */
    public void deleteSetMeal(List<Long> ids) {
        //需要删除setmeal以及setmealDishes在内的对应的
        //先检查套餐是否能够被删除，也就是是否正在售卖
        int Number =  setmealMapper.CouldBeDeleted(ids);
        Output.Output(Number,"deleteSetMeal");
        if (Number !=  0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //删除setmealDishes中的对应条目
        setmealDishMapper.deleteBysetMealId(ids);
        //删除setmeal中的数据
        setmealMapper.deleteByDeleteMealId(ids);
    }

    /**
     * 改变套餐起售停售状态
     * @param status
     * @param id
     */
    @Override
    public void changeStatus(int status, Long id) {
        setmealMapper.changeStatus(status,id);
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
